package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {
    public static String url = "http://dongliyang.online:10030/oj/login";

    public LoginPage() throws IOException, InterruptedException {
        super(url);
    }

    // 登录成功并跳转
    public void loginSuccess() throws IOException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // 等待页面加载完成
//        wait.until(ExpectedConditions.urlContains("/c-oj/login"));
        System.out.println("成功跳转到登录页面，当前 URL: " + driver.getCurrentUrl());
        getScreenShot("loginPageLoaded");

        // 输入用户名
        WebElement usernameInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div/div/div/div[4]/div[2]/div[1]/div/div/input")
        ));
        usernameInput.sendKeys("Admin@123");

        // 输入密码
        WebElement passwordInput = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div/div/div/div[4]/div[2]/div[2]/div/div/input")
        ));
        passwordInput.sendKeys("123456");

        // 点击登录按钮
        WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("/html/body/div/div/div/div[4]/div[2]/div[3]")
        ));
        loginButton.click();

        // 等待跳转到题目管理页面
        wait.until(ExpectedConditions.urlContains("/oj/layout/question"));
        assert driver.getCurrentUrl().contains("/oj/layout/question") : "登录后未跳转到题目管理页面";
        System.out.println("登录成功，跳转到题目管理页面。当前 URL: " + driver.getCurrentUrl());
        getScreenShot("loginSuccess");
    }
}